package com.shipwe.loans.utils.helper

import com.shipwe.language.LocaleContract
import com.shipwe.language.ShipweLanguages
import com.shipwe.loans.MyApps
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import org.json.JSONArray
import org.json.JSONObject
import kotlin.random.Random

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/28 12:04
 * @Annotation：自定义数据 帮助类
 */
object CustomConfigsHelper {

    /**
     * 自定义数据来源
     *     1：客服配置
     *
     *     "customData": {
     *         "privacyPolicyLink": "https://www.baidu.com",
     *         "contactPonits": "2000",
     *         "accountPonits": "2500",
     *         "loanApplyPonits": "5000",
     *         "birthdayPonits": [
     *             "8500",
     *             "17000",
     *             "26000"
     *         ],
     *         "workDate": "Monday to Sunday",
     *         "workTime": "9:30am to 19:30pm.",
     *         "signPoints": [
     *             "100",
     *             "200",
     *             "520",
     *             "340",
     *             "430",
     *             "520",
     *             "850"
     *         ],
     *         "taskPoints": [
     *             "850",
     *             "4300",
     *             "8500"
     *         ],
     *         "luckyDrawUse": "1800",
     *         "luckyDrawGift": [
     *             "900",
     *             "3600"
     *         ],
     *         "changeRate": "0.01",
     *         "discountRate": "0.1"
     *     },
     *
     */

    /**
     * 获取客服配置
     */
    fun getShipweConfigData(): JSONObject? {
        val dataString = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun getShipweCustomData(): JSONObject?{
        val customize = getShipweConfigData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 获取启动权限
     */
    fun getShipwePermissionsLaunch(): JSONArray?{
        val customize = getShipweConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                return permissions.optJSONArray("permission")
            }
        }
        return null
    }

    /**
     * 获取首页权限
     */
    fun getShipwePermissionsHome(): JSONArray?{
        val customize = getShipweConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                return permissions.optJSONArray("home")
            }
        }
        return null
    }

    /**
     * 获取所有权限列表 = 启动权限 + 首页权限
     */
    fun getShipwePermissionsList(): ArrayList<String>{
        val permissionList = arrayListOf<String>()
        //启动权限
        val permissionLaunch = getShipwePermissionsLaunch()
        if (permissionLaunch != null){
            for (i in 0..<permissionLaunch.length()){
                permissionList.add(permissionLaunch.optString(i))
            }
        }

        //首页权限
        val permissionHome = getShipwePermissionsHome()
        if (permissionHome != null){
            for (i in 0..<permissionHome.length()){
                val str = permissionHome.optString(i)
                if (permissionList.contains(str)){
                    continue
                }
                permissionList.add(str)
            }
        }
        return permissionList
    }

    /**
     * 获取隐私协议链接
     */
    fun getShipwePrivacyPolicyLink(): String{
        val customData = getShipweCustomData() ?: return ""
        return customData.optString("privacyPolicyLink","")
    }

    /**
     * 获取用户使用协议
     */
    fun getShipweTermsUseLink(): String{
        val customData = getShipweCustomData() ?: return ""
        if (customData.has("termsUseLink")){
            return customData.optString("termsUseLink","")
        }
        return ""
    }

    /**
     * 是否显示清空数据按钮
     */
    fun isShipweClearUserData(): Boolean{
        val customData = getShipweCustomData() ?: return false
        if (customData.has("isClearData")){
            return customData.optString("isClearData","NO") == "YES"
        }
        return false
    }

    /**
     * 获取牌照信息
     */
    fun getShipweLicenseInfo(): JSONObject?{
        val customData = getShipweCustomData() ?: return null
        var licenseData: JSONObject? = null
        if (customData.has("licenseInfo")){
            licenseData = customData.optJSONObject("licenseInfo")
        }

        //是否是 斯瓦希里语
        val curtLocale = ShipweLanguages.getAppLanguage(MyApps.app.getAppContext())
        if (customData.has("licenseInfo_sw") && curtLocale.language == LocaleContract.getSwahiliLocale().language){
            licenseData = customData.optJSONObject("licenseInfo_sw")
        }
        return licenseData
    }

    /**
     * 是否显示牌照信息
     */
    fun isShipweShowLicense(): Boolean{
        val licenseInfo = getShipweLicenseInfo() ?: return false
        return licenseInfo.optString("isShow") == "YES"
    }

    /**
     * 审核中、放款中是否显示公告信息
     */
    fun isShipweAuditNotice(): Boolean{
        val customData = getShipweCustomData() ?: return false
        if (customData.has("noticeInfo")){
            val noticeInfo = customData.optJSONObject("noticeInfo") ?: return false
            return noticeInfo.optString("isShow","NO") == "YES"
        }
        return false
    }

    /**
     * 获取 审核中、放款中 显示的公告信息
     */
    fun getShipweAuditNoticeInfo(): JSONObject?{
        val customData = getShipweCustomData() ?: return null

        var noticeData: JSONObject? = null
        if (customData.has("noticeInfo")){
            //默认为英语
            noticeData = customData.optJSONObject("noticeInfo")
        }

        //是否是 斯瓦希里语
        val curtLocale = ShipweLanguages.getAppLanguage(MyApps.app.getAppContext())
        if (customData.has("noticeInfo_sw") && curtLocale.language == LocaleContract.getSwahiliLocale().language){
            noticeData = customData.optJSONObject("noticeInfo_sw")
        }
        return noticeData
    }

    /**
     * SMS Content 关键字过滤
     */
    fun getShipweSmsContentKeys(): MutableSet<String>?{
        val customData = getShipweCustomData() ?: return null
        if (customData.has("smsContentKeys")){
            val smsContentKeys = customData.optJSONArray("smsContentKeys")
            if (smsContentKeys != null) {
                val keysSet = mutableSetOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysSet.add(smsContentKeys.optString(i))
                }
                return keysSet
            }
        }
        return null
    }


    /**
     * SMS Mobile 关键字过滤
     */
    fun getShipweSmsMobileKeys(): MutableSet<String>?{
        val customData = getShipweCustomData() ?: return null
        if (customData.has("smsMobileKeys")){
            val smsContentKeys = customData.optJSONArray("smsMobileKeys")
            if (smsContentKeys != null) {
                val keysSet = mutableSetOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysSet.add(smsContentKeys.optString(i))
                }
                return keysSet
            }
        }
        return null
    }

    /**
     * 获取添加联系人对应获得的积分
     */
    fun getShipweAddContactPoints(): String{
        var showPoints = "0"
        val customData = getShipweCustomData()
        if (customData != null) {
            showPoints = customData.optString("contactPonits")
        }
        return showPoints
    }

    /**
     * 获取添加绑卡账户对应获得的积分
     */
    fun getShipweAddWalletPoints(): String{
        var showPoints = "0"
        val customData = getShipweCustomData()
        if (customData != null) {
            showPoints = customData.optString("accountPonits")
        }
        return showPoints
    }

    /**
     * 获取贷款申请对应获得的积分
     */
    fun getShipweLoanApplyPoints(): String{
        var showPoints = "0"
        val customData = getShipweCustomData()
        if (customData != null) {
            showPoints = customData.optString("loanApplyPonits")
        }
        return showPoints
    }

    /**
     * 根据签到天数 获取对应积分
     * 注意：签到天数 > 7 :
     *          可获得积分数 = 签到天数 % 7 - 1
     *      签到天数 <= 7:
     *          可获得积分数 = 签到天数 - 1
     */
    fun getShipweSignPointsBySignTime(signTime: Int): String{
        val customData = getShipweCustomData()
        if (customData != null){
            val signPoints = customData.optJSONArray("signPoints") ?: return "0"
            var signLength = signTime
            if (signLength > 7){
                signLength %= 7
            }
            if (signLength == 0){
                signLength = 7
            }
            return signPoints.optString(signLength - 1)
        }
        return "0"
    }

    /**
     * 获取任务积分
     */
    fun getShipweTaskPointsByTaskId(taskId: Int): String{
        val customData = getShipweCustomData()
        if (customData != null){
            val taskPoints = customData.optJSONArray("taskPoints") ?: return "0"
            return taskPoints.optString(taskId)
        }
        return "0"
    }

    /**
     * 获取抽奖需要消耗的积分
     */
    fun getShipweLuckyDrawPointsUse(): String{
        val customData = getShipweCustomData()
        if (customData != null) {
            return customData.optString("luckyDrawUse")
        }
        return "0"
    }

    /**
     * 获取抽奖获得的随机积分
     *
     * 规则：80% 奖励1
     *      20% 奖励2
     */
    fun getShipweLuckyDrawPoints(): String{
        val customData = getShipweCustomData()
        if (customData != null) {
            val pointsList = customData.optJSONArray("luckyDrawGift") ?: return "0"
            val randomIndex = Random.nextInt(100)
            if (randomIndex <= 80){
                return pointsList.optString(0)
            } else {
                return pointsList.optString(1)
            }
        }
        return "0"
    }

    /**
     * 根据VIP等级 获取生日福利对应的积分
     */
    fun getShipweBirthdayBenefitsPoints(): String{
        val vip = getShipweUserVip()
        val customData = getShipweCustomData()
        if (customData != null) {
            val birthdayPonits = customData.optJSONArray("birthdayPonits") ?: return "0"
            return birthdayPonits.optString(vip - 1)
        }
        return "0"
    }

    /**
     * 获取用户VIP等级 1,2,3
     */
    fun getShipweUserVip(): Int{
        //用户VIP等级在首页获取产品列表处保存
        val vip = ShipweSharedHelper.getShipweSharedInt(ShipweSharedKey.SHIPWE_VIP)
        if (vip == 0){
            return 1
        }
        return vip
    }

    /**
     * 获取积分抵扣金额的比例
     */
    fun getShipweChangeRate(): String{
        var showChangeRate = "0.01"
        val customData = getShipweCustomData()
        if (customData != null) {
            showChangeRate = customData.optString("changeRate")
        }
        return showChangeRate
    }

    /**
     * 获取可使用积分抵扣的最大金额比例
     */
    fun getShipweDiscountRate(): String{
        var showDiscountRate = "0.1"
        val customData = getShipweCustomData()
        if (customData != null) {
            showDiscountRate = customData.optString("discountRate")
        }
        return showDiscountRate
    }

}